/**
 * 浏览器检查模块
 * @exports CFBrowser
 * @constructor CFBrowser
 * @description 调用方法：$cf.CFBrowser.函数名称
 */

class CFBrowser {
  constructor() {
    this.userAgent = navigator.userAgent.toLowerCase();
  }
  /**
   * 是否是ie浏览器
   * @returns {Boolean}
   * @example
   * $cf.CFBrowser.isIE(); // false
   */
  isIE() {
    return !!this.userAgent.match(/trident|msie/i);
  }
  /**
   * 获取浏览器详细信息
   * @returns {Object} 浏览器类型和版本
   * @example
   * $cf.CFBrowser.getBrowserInfo(); // {type: IE, version: 11}
   */
  getBrowserInfo() {
    let type;
    let version;
    let userAgent = this.userAgent;
    if (userAgent.indexOf('msie') !== -1) {
      // IE < 11
      type = 'IE';
      version = Number(userAgent.match(/msie ([\d]+)/)[1]);
    } else if (userAgent.indexOf('trident') !== -1) {
      // IE11
      type = 'IE';
      version = Number(userAgent.match(/rv:([\d]+)/)[1]);
    } else if (userAgent.indexOf('edg') !== -1) {
      // Edge
      type = 'Edge';
      version = Number(userAgent.match(/edge?\/([\d]+)/)[1]);
    } else if (userAgent.indexOf('opera') !== -1) {
      // Opera
      type = 'Opera';
      version = Number(userAgent.match(/opera.([\d]+)/)[1]);
    } else if (userAgent.indexOf('chrome') !== -1) {
      // Chrome
      type = 'Chrome';
      version = Number(userAgent.match(/chrome\/([\d]+)/)[1]);
    } else if (userAgent.indexOf('firefox') !== -1) {
      // FireFox
      type = 'FireFox';
      version = Number(userAgent.match(/firefox\/([\d]+)/)[1]);
    } else if (userAgent.indexOf('safari') !== -1) {
      // Safari（匹配Safari字段并且没有Chrome字段）
      type = 'Safari';
      version = Number(userAgent.match(/safari\/([\d]+)/)[1]);
    } else {
      // 未知类型
      type = 'unknow';
      version = 'unknow';
    }
    return { type, version };
  }
  /**
   * 获取操作系统
   * @returns {String} 当前设备操作系统
   * @example
   * $cf.CFBrowser.getOs(); // Windows
   */
  getOs() {
    let type;
    let userAgent = this.userAgent;
    if (userAgent.indexOf('windows') !== -1) {
      type = 'Windows';
    } else if (userAgent.indexOf('android') !== -1) {
      type = 'Android';
    } else if (userAgent.indexOf('linux') !== -1) {
      // 包含linux字段不包含android字段
      type = 'Linux';
    } else if (userAgent.indexOf('iphone') !== -1) {
      type = 'iPhone';
    } else if (userAgent.indexOf('ipad') !== -1) {
      type = 'iPad';
    } else if (userAgent.indexOf('mac') !== -1) {
      type = 'Mac';
    } else {
      type = 'unknow';
    }
    return type;
  }
  /**
   * 获取页面视口大小
   * @returns {Object} 页面视口宽度和高度
   * @example
   * $cf.CFBrowser.getPageWH(); // {width: 1422, height: 268}
   */
  getPageWH() {
    let width = window.innerWidth;
    let height = window.innerHeight;
    if (typeof width !== 'number') {
      // 页面是否处于标准模式
      if (document.compatMode === 'CSS1Compat') {
        width = document.documentElement.clientWidth;
        height = document.documentElement.clientHeight;
      } else {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
      }
    }
    return {
      width,
      height
    };
  }
  /**
   * 获取浏览器导航地址查询参数
   * @returns {Object} 查询参数对象
   * @example
   * $cf.CFBrowser.getQueryParams(); // {id: 1}
   */
  getQueryParams() {
    let result = {};
    // 导航栏搜索以?开头
    let paramsUrl = location.search.length ? location.search.substring(1) : '';
    let paramsArr = paramsUrl.split('&');
    paramsArr.forEach((item) => {
      let itemArr = item.split('=');
      let name = decodeURIComponent(itemArr[0]);
      let value = decodeURIComponent(itemArr[1]);
      result[name] = value;
    });
    return result;
  }
}

export default CFBrowser;
